﻿@model Models.NewsContent

<!DOCTYPE html>

<html>
<head>
    <title>添加用户信息</title>
    <link href="~/Content/tableStyle.css" rel="stylesheet" />
    @Scripts.Render("~/mvcAjax")
    @*实现异步上传图片的功能*@
    <script src="~/Scripts/MyAjaxForm.js"></script>

    @*引入富文本编辑器的使用*@
    <script src="~/kindeditor-4.1.3/kindeditor.js"></script>
    <script src="~/kindeditor-4.1.3/lang/zh_CN.js"></script>
    <script src="~/kindeditor-4.1.3/plugins/code/prettify.js"></script>
    <script type="text/javascript">
        @*父文本框的使用*@
        KindEditor.ready(function (K) {
            //实现显示富文本的代码，第一个方法,实现添加的框架
            window.editorAdd = K.create('#content', {
                cssPath: '../../kindeditor-4.1.3/plugins/code/prettify.css',
                uploadJson: 'upload_json.ashx',
                fileManagerJson: 'file_manager_json.ashx',
                allowFileManager: true,
                afterCreate: function () {
                    var self = this;
                    K.ctrl(document, 13, function () {//添加保存信息的组合键ctrl+z
                        self.sync();//同步富文本编辑器里的内容到textarea中去
                        K('form[name="AddNewsContent"]')[0].submit();
                    });
                    K.ctrl(self.edit.doc, 13, function () {
                        self.sync();
                        K('form[name="AddNewsContent"]')[0].submit();
                    });
                }
            });
            //不知道什么用处，暂时添加在这里
            prettyPrint();

            //获取添加文本框内容
            function getEditorAddData() {
                return editorAdd.html();     //使用上面定义的window.editorAdd的方法的HTML代码
            }

            //实现操作Jquery的方法
            $(function () {
                //定义参数保存路径
                var imgFileName;
                //实现异步上传图片
                $("#btnFileImg").click(function () {
                    $("#frmUploadImg").ajaxSubmit({
                        URL: "/MainDetail/FileUpload",
                        type: "post",
                        success: function (imgFileData) {
                            var arr = imgFileData.split("|")
                            if (arr[0] == "ok") {

                                imgFileName = arr[1];
                                $("#newsImage").val(imgFileName);
                                $("#result").attr("src", "../../upImg/" + arr[1]);
                            }
                            else {
                                alert(arr[1]);
                            }

                        }
                    });
                });

                //实现添加新闻的信息
                $("#btnAddMain").click(function () {
                    //txtMainTitle，content，txtMainDateTime，txtMainPeople，txtMainPic
                    //获得要传递的参数
                    var MainData = {
                        title: $("#title").val(), content: getEditorAddData(),
                        newsSource: $("#newsSource").val(), newsTypeid: $("#newsTypeid").val(),
                        newsImage: $("#newsImage").val(), id: $("#id").val()
                    };

                    $.ajax({
                        url: "/News/Editnews",
                        data: MainData,
                        type: "post",
                        success: function (data) {
                            if (data.Statu == "ok") {
                                alert("修改成功，正在跳转中");
                                window.location.href = "../news/newsview";
                            }
                            else {
                                alert("修改失败，请您检查填写完整");
                            }
                        }
                    });
                });
            });
        });


    </script>
</head>
<body>
    <form id="frmUploadImg" enctype="multipart/form-data" action="/news/FileUpload">
        <div>
            <input type="hidden" name="id" id="id" value="@Model.id" />


            <table>
                <tr>
                    <th colspan="2">添加新闻列表信息</th>
                </tr>
                <tr>
                    <td>标题：</td>
                    <td>
                        @Html.TextBoxFor(p => p.title, new { style = "width:400px;" })
                        @Html.ValidationMessageFor(p => p.title)
                    </td>
                </tr>
                <tr>
                    <td>内容：</td>
                    <td>
                        <textarea id="content" name="content" style="width: 550px;
                            height: 300px; visibility: hidden;">
                            @Model.content
                        </textarea>
                    </td>
                </tr>

                <tr>
                    <td>来源：</td>
                    <td>
                        @Html.TextBoxFor(p => p.newsSource)
                    </td>
                </tr>
                <tr>
                    <td>新闻类型：</td>
                    <td>
                        @Html.DropDownListFor(p => p.newsTypeid, ViewBag.newstype as IEnumerable<SelectListItem>)
                    </td>
                    @Html.ValidationMessageFor(p => p.newsTypeid)
                </tr>
                <tr>
                    <td>图片：</td>
                    <td>
                        <input type="file" name="txtMainPic" id="txtMainPic" />
                        <input type="button" name="btnFileImg" id="btnFileImg" value="上传" />

                    </td>
                </tr>
                <tr>
                    <td>图片显示：</td>
                    <td>

                        @if (@Model.newsImage == "" || @Model.newsImage == null)
                        {
                            @Html.Raw("   <img id='result' width='200' height='150' src='../../upimg/noimg.jpg' />")
                        }
                        else
                        {
                            @Html.Raw("   <img id='result' width='200' height='150' src='../../upimg/" + @Model.newsImage + "'/>")
                        }

                        <input type="hidden" name="newsImage" id="newsImage" value="@Model.newsImage" />
                    </td>
                </tr>
                <tr>

                    <td colspan="2" align="center">
                        <input type="button" name="btnAddMain" value="保存" id="btnAddMain" />&nbsp;&nbsp;&nbsp;
                        <input type="button" value="返回" id="back" onclick="window.location='../news/newsview'" />

                    </td>

                </tr>
            </table>
        </div>
    </form>
</body>
</html>
